﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.API.Translate;

namespace GoogleTranslatorWS
{
    public class GoogleTranslaorWS
    {
        //http://code.google.com/p/google-api-for-dotnet/

        /*
        Arabic - Saudia Arabia ...1025 
        Chinese - PRC ...2052 
        Chinese - Taiwan ...1028 
        Dutch ...1043 
        English - United States ...1033 
        French - Standard ...1036 
        German - Standard ...1031 
        Italian - Standard ...1040 
        Japanese ...1041 
        Korean ...1042 
        Portuguese - Brazil ...1046 
        Spanish - Standard ...1034 
        */
        /*
        private static Language lidToGoogle(string lcid)
        {
            switch (lcid)
            {
                case "1025":
                    return Language.Arabic;
                case "2052":
                    return Language.Chinese;
                case "1028":
                    return Language.ChineseSimplified;
                case "1043":
                    return Language.Dutch;
                case "1033":
                    return Language.English;
                case "1036":
                    return Language.French;
                case "1031":
                    return Language.German;
                case "1040":
                    return Language.Italian;
                case "1041":
                    return Language.Japanese;
                case "1042":
                    return Language.Korean;
                case "1046":
                    return Language.Portuguese;
                case "1034":
                    return Language.Spanish;
                default:
                    return Language.Unknown;
            }
        }
        */
        private static string lidToGoogle(string lcid)
        {
            switch (lcid)
            {
                case "1025":
                    return "ar";
                case "2052":
                    return "zh";
                case "1028":
                    return "zh-CN";
                case "1043":
                    return "nl";
                case "1033":
                    return "en";
                case "1036":
                    return "fr";
                case "1031":
                    return "de";
                case "1040":
                    return "it";
                case "1041":
                    return "ja";
                case "1042":
                    return "ko";
                case "1046":
                    return "pt-PT";
                case "1034":
                    return "es  ";
                default:
                    return String.Empty;
            }
        }

        public static string Translate(string text, string from_lcid, string to_lcid, out bool status)
        {
            string translated = String.Empty;
            status = true;
            try
            {
                TranslateClient client = new TranslateClient(@"http://www.google.com");
                translated = client.Translate(text, lidToGoogle(from_lcid), lidToGoogle(to_lcid));
            }
            catch
            {
                    status = false;
            }
            return translated;
        }

    }
}
